package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.WorkExperienceAdapter;
import com.wbao.dianniu.customView.MyListView;
import com.wbao.dianniu.data.WorkExperienceData;
import com.wbao.dianniu.listener.IJobWorkListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobWorkListManager;
import com.wbao.dianniu.update.WorkListChangeManager;
import com.wbao.dianniu.update.WorkListChangeManager.WorkChangedListener;
import com.wbao.dianniu.R;


/**
 * 编辑工作经历
 *
 * @author yejianjun
 */
public class EditJobExActivity extends BaseActivity implements IJobWorkListListener, WorkChangedListener {

    private MyListView mListView;
    private Button addBtn;
    private JobWorkListManager manager;
    private WorkExperienceAdapter adapter;
    private List<WorkExperienceData> mList = new ArrayList<WorkExperienceData>();
    private WorkListChangeManager workListChangeManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.edit_job_experience, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        setTitleName(getResources().getString(R.string.work_experience));
        init();
        initData();
        manager = new JobWorkListManager(this);
        manager.addJobWorkListListener(this);
        manager.jobWorkList(GlobalContext.getAccountId());
        workListChangeManager = WorkListChangeManager.getInstance();
        workListChangeManager.addWorkChangeListener(this);
    }

    private void init() {
        mListView = (MyListView) findViewById(R.id.jobEx_list);
        addBtn = (Button) findViewById(R.id.edit_add_experience);
        addBtn.setOnClickListener(this);
    }

    private void initData() {
        adapter = new WorkExperienceAdapter(this, 2);
        adapter.setData(mList);
        mListView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(mListView);
    }

    @Override
    public void onJobWorkListFailure(int errorCode, String errorMsg) {

    }

    @Override
    public void onJobWorkListSuccess(List<WorkExperienceData> list) {
        mList = list;
        adapter.setData(list);
        setListViewHeightBasedOnChildren(mListView);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.edit_add_experience:
                Intent intent = new Intent();
                intent.setClass(this, AddJobExActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter   
        int totalHeight = 0;
        for (int i = 0, len = adapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目   
            View listItem = adapter.getView(i, null, listView);
            // 计算子项View 的宽高   
            listItem.measure(0, 0);
            // 统计所有子项的总高度   
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度   
        // params.height最后得到整个ListView完整显示需要的高度   
        listView.setLayoutParams(params);
    }

    @Override
    public void workAddChanged(WorkExperienceData data) {
        // TODO Auto-generated method stub
        mList.add(data);
        adapter.setData(mList);
        setListViewHeightBasedOnChildren(mListView);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void workRemoveChanged(int position) {
        mList.remove(position);
        adapter.setData(mList);
        setListViewHeightBasedOnChildren(mListView);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void workModifyChanged(int id, WorkExperienceData data) {
        for (int i = 0; i < mList.size(); i++) {
            if (id == mList.get(i).getId()) {
                mList.get(i).setPost(data.getPost());
                mList.get(i).setStartDate(data.getStartDate());
                mList.get(i).setEndDate(data.getEndDate());
                mList.get(i).setContent(data.getContent());
                break;
            }
        }
        setListViewHeightBasedOnChildren(mListView);
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != workListChangeManager) {
            workListChangeManager.removeWorkChangeListener(this);
        }
        if (null != manager) {
            manager.removeJobWorkListListener(this);
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
